package com.alibaba.org.service.impl;

import com.alibaba.basic.util.PageList;
import com.alibaba.org.domain.Department;
import com.alibaba.org.mapper.DepartmentMapper;
import com.alibaba.org.query.DepartmentQuery;
import com.alibaba.org.service.DepartmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门实现类
 */
//ctrl + t 跳到service层
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> loadAll() {
        return departmentMapper.loadAll();
    }

    @Override
    public Department loadById(Long id) {
        return departmentMapper.loadById(id);
    }
    @Transactional
    @Override
    public void save(Department department) {
        //处理dirPath
        //1.没有选上级部门
        //Department( manager_id=327, manager=null, parent_id=null, parent=null, children=[])
        //2.选了上级部门
        //Department( manager_id=327, manager=null, parent_id=4, parent=null, children=[])
        //3.选了之后叉掉
        //Department( manager_id=326, manager=null, parent_id=null, parent=null, children=[])
        //添加之后会返回一个自增长id
        departmentMapper.save(department);

        //添加的时候对dirPath的处理
        String dirPath = "";
        if(department.getParent_id() == null){//没有选择上级部门 or //选了之后×掉了
            //获取自增id
            Long id  = department.getId();
            dirPath = "/" + id;
        }else{
            //获取上级部门
            Long parent_id = department.getParent_id();
            //获取上级部门的dirPath
            String parentDirPath = departmentMapper.loadById(parent_id).getDirPath();
            dirPath = parentDirPath + "/" + department.getId();
        }

        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }
    @Transactional
    @Override
    public void remove(Long id) {
        departmentMapper.remove(id);
    }
    @Transactional
    @Override
    public void update(Department department) {
        //添加的时候对dirPath的处理
        String dirPath = "";
        if(department.getParent_id() == null){//没有选择上级部门 or //选了之后×掉了
            Long id  = department.getId();
            dirPath = "/" + id;
        }else{
            //获取上级部门
            Long parent_id = department.getParent_id();
            //获取上级部门的dirPath
            String parentDirPath = departmentMapper.loadById(parent_id).getDirPath();
            dirPath = parentDirPath + "/" + department.getId();
        }
        //设置新的dirPath
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    public PageList<Department> queryPage(DepartmentQuery departmentQuery) {

        Long count = departmentMapper.queryCount(departmentQuery);
        if (count == 0){
            return new PageList<>();
        }

        List<Department> departments = departmentMapper.queryData(departmentQuery);

        return new PageList<>(count,departments);
    }

    @Transactional
    @Override
    public void pathDelete(Long[] ids) {
        departmentMapper.pathDelete(ids);
    }

    /**
     * 一次性查询出所有数据,放到内存中,可以使用map
     * 从所有数据中找出第一层级/顶级部门 即我们需要返回的数据
     * 遍历所有部门只要不是第一层级的 就去查找自己上级 并将自己放入上级children中
     * @return
     */
    @Override
    public List<Department> getDeptTree() {
        List<Department> all = departmentMapper.loadAll();
        // 用这个集合装所有的Department
        Map<Long, Department> map = new HashMap<>();
        // foreach快捷键 --- iter
        for (Department department : all) {
            map.put(department.getId(),department);
        }
        //前端需要得到的集合数据 - 部门树
        List<Department> deptTree = new ArrayList<>();
        for (Department department:all){
            if(department.getParent_id() == null){//顶级部门
                // 父部门直接装进去
                deptTree.add(department);
            }else{//不是顶级部门
                //1.想办法找到当前部门的 父部门
                Long parent_id = department.getParent_id();
                //这种从数据库查询的方式，每次通过id去查询 - 查询出来映射给实体domain，children集合中都会是空集合 - 没有任何数据
                //Department parentDepartment = departmentMapper.loadById(parent_id);
                //下面这种是从Map中去获取的 - 以前装进去的对象还是会存在
                Department parentDepartment = map.get(parent_id);
                //2.将自己装到  父部门 的children中
                parentDepartment.getChildren().add(department);
            }
        }
        return deptTree;
    }
}
